//短信下发
const express = require('express');
const router = express.Router();
const axios = require('axios');
const client = require('./Client');

/* GET home page. */
router.post('/', async (req, res, next) => {
    try {
        // 1. 获取请求头和请求体
        const xAGCAuth = req.headers['x-agc-auth'];
        const xAGCTimestamp = req.headers['x-agc-timestamp'];
        const thirdSmsUrl = req.originalUrl; // 或完整URL
        const thirdSmsReq = req.body;

        // 2. 执行签名验证
        const isValid = await verifyAuth(
            xAGCAuth,
            xAGCTimestamp,
            thirdSmsUrl,
            thirdSmsReq
        );

        // 3. 处理验证结果
        if (!isValid) {
            return res.status(401).json({ error: "签名验证失败" });
        }

        // 4. 验证通过后的业务逻辑
        await client.main(thirdSmsReq.verifyCode)
        res.status(200).json({ code: "0", message:"OK",requestId:thirdSmsReq.taskId});

    } catch (err) {
        console.error("处理失败:", err.message);
        res.status(400).json({error: err.message});
    }
});

module.exports = router;